var jqSortable = function(){
	this.selector = null;
	this.opts = null;
	this.oldContainer = null;
};

jqSortable.prototype.setData = function (item, idx){
	var jh = new jsonHelper2();
	var objToString = jh.jsonObjToString(item);
	var debug = "hidden";//hidden //text
	var li = $("<li />").attr("id", item.fnId);
	var fnAlias = $("<span />").text(item.fnAlias);
	var grpId = $("<input />").attr("size", "4").attr("type", debug).attr("name", "items["+(idx)+"].grpId").attr("value", item.grpId);
	var fnId = $("<input />").attr("size", "4").attr("type", debug).attr("name", "items["+(idx)+"].fnId").attr("value", item.fnId);
	var fnParentId = $("<input />").attr("size", "4").attr("type", debug).attr("name", "items["+(idx)+"].fnParentId").attr("value", item.fnParentId);
	var fnIdx = $("<input />").attr("size", "4").attr("type", debug).attr("name", "items["+(idx)+"].fnIdx").attr("value", item.fnIdx);
	var fnParentIdx = $("<input />").attr("size", "4").attr("type", debug).attr("name", "items["+(idx)+"].fnParentIdx").attr("value", item.fnParentIdx);
	var fnOrder = $("<input />").attr("size", "4").attr("type", debug).attr("name", "items["+(idx)+"].fnOrder").attr("value", item.fnOrder);
	var fnGroupId = $("<input />").attr("size", "4").attr("type", debug).attr("name", "items["+(idx)+"].fnGroupId").attr("value", item.fnGroupId);
	var fnUseYN = $("<input />").attr("size", "4").attr("type", debug).attr("name", "items["+(idx)+"].fnUseYN").attr("value", item.fnUseYN);
	var fnChmod = $("<input />").attr("size", "2").attr("type", debug).attr("name", "items["+(idx)+"].fnChmod").attr("value", item.fnChmod).css("font-size", "10px");
	var fnServletUrl = $("<input />").attr("size", "4").attr("type", debug).attr("value", item.fnServletUrl);
	var fnToString = $("<input />").attr("size", "4").attr("type", debug).attr("value", objToString);
	var ol = $("<ol />").css("border", "0");
	var child = $(li);
	child.append($(fnAlias));
	child.append($(grpId));
	child.append($(fnId));
	child.append($(fnParentId));
	child.append($(fnIdx));
	child.append($(fnParentIdx));
	child.append($(fnOrder));
	child.append($(fnGroupId));
	child.append($(fnUseYN));
	child.append("&nbsp;");
	child.append($(fnChmod));
	child.append($(fnServletUrl));
	child.append($(fnToString));
	child.append($(ol));
	
	return child;
};

jqSortable.prototype.init = function(opts){
	this.xWindow = new Browser();
	this.opts = opts;
	this.selector = this.opts["selector"];
};

jqSortable.prototype.isEmpty = function(){
	var rtnValue = false;
	var obj = this.selector;
	var liObj = $(obj).find("li");

	if($(liObj).length <= 0){
		rtnValue = true;
	}else{
		if($(liObj).length == 1 && $(liObj).attr("class") == "nothing"){
			rtnValue = true;
		}
	}
	
	return rtnValue;
};

jqSortable.prototype.genData = function(data){
	var thisObject = this;
	var sortableClass = thisObject.opts["sortableClass"];
	var rootOlTag = $("<ol class='"+sortableClass+"' />");

	if(this.xWindow.isIE && this.xWindow.version < 9.0){
		for(key in data) {
			if(data.hasOwnProperty(key)) {
				var idx = 0;
				for(var o in data[key]){
					if(data[key].hasOwnProperty(o)) {
						var item = data[key][o];
						var child = thisObject.setData(item, idx);
						
						if(item.fnParentId == "0"){
							$(rootOlTag).append($(child));
						}else{
							var target = $(rootOlTag).find("#"+item.fnParentId);
							var targetOl = $(target).find(">ol");
							$(targetOl).append($(child));						
						}
						
						idx++;
					}
				}
			}else{
				var li = $("<li />").attr("class", "nothing").text("nothing.");
				$(rootOlTag).append($(li));
			}
		}
	}else{
		Object.keys(data).forEach(function(key){
			if(data[key].length){
				data[key].forEach(function(item, idx){
					var child = thisObject.setData(item, idx);

					if(item.fnParentId == "0"){
						$(rootOlTag).append($(child));
					}else{
						var target = $(rootOlTag).find("#"+item.fnParentId);
						var targetOl = $(target).find(">ol");
						$(targetOl).append($(child));						
					}
				});
			}else{
				var li = $("<li />").attr("class", "nothing").text("nothing.");
				$(rootOlTag).append($(li));
			}
		});
	}
	
	var target = $(this.opts["resultTargetObj"]);
	$(target).html("");
	$(target).html($(rootOlTag));
};

jqSortable.prototype.getSortable = function(){
	var thisObject = this;
	var obj = this.selector;
	var viewUrl = thisObject.opts["viewUrl"];
	$(obj).sortable({
		//group: 'nested',
		afterMove: function (placeholder, container) {
			if(this.oldContainer != container){
				if(this.oldContainer) this.oldContainer.el.removeClass("active");
				
				container.el.addClass("active");
				this.oldContainer = container;
			}
		},
		onDrop: function (item, container, _super, event) {
			container.el.removeClass("active");
			_super(item);
			
			thisObject.reBuild($(obj));
		},
		onMousedown: function (item, _super, event){
			var rtnValue = true;
			var chkSameObj = false;
			var isPrevOpt = false;
			var fnId = $(item).find(">input").eq(1).val();
			
			if($("input[name='soptRadio']:checked").val() == "1"){
				isPrevOpt = true;
				rtnValue = false;
			}
			
			$(obj).find("li[class='selectedLi']").each(function(idx, child){
				if(fnId == $(child).find(">input").eq(1).val()){
					chkSameObj = true;
				}else{
					$(child).removeClass("selectedLi");
				}
			});	
			
			if(!chkSameObj){
				if(fnId != null && fnId != "undefined" && fnId != ""){
					$(item).addClass("selectedLi");
					thisObject.opts["viewUrl"] = viewUrl+"/"+fnId;
					thisObject.getView();
				}
			}
			
			if(!isPrevOpt) event.preventDefault();
			
			return rtnValue;
		}
	});	
};

jqSortable.prototype.setOption = function(event){
	var obj = this.selector;
	
	$(obj).find("li").each(function(idx, item){
		var optSelector = $(item).find(">input").eq(8);
		if($(optSelector).attr("type") == "text"){
			$(optSelector).attr("type", "hidden");
		}else{
			$(optSelector).attr("type", "text");
		}
	});
};

jqSortable.prototype.reBuild = function(obj){
	var selector = obj;
	var fnIdx = 1;
	$(selector).find("li").each(function(idx, item){
		var order = $(this).index();	
		var selfInputTag = $(this).find(">input");
		var parentLiTag = $(this).parent("ol").parent("li");
		
		$(selfInputTag).eq(3).val(fnIdx++);//fnIdx
		$(selfInputTag).eq(5).val(order);//fnOrder
		
		if($(parentLiTag).length <= 0){
			var fnGroupId = $(selfInputTag).eq(1).val();//fnGroupId
			$(selfInputTag).eq(2).val("0");//fnParentId
			$(selfInputTag).eq(4).val("0");//fnParentIdx
			$(selfInputTag).eq(6).val(fnGroupId);//fnGroupId
		}else{
			var parentInputTag = $(parentLiTag).find(">input");
			var fnParentId = $(parentInputTag).eq(1).val();//parent-fnId
			var fnParentIdx = $(parentInputTag).eq(3).val();//parent-fnIdx
			var fnGroupId =  $(parentInputTag).eq(6).val();//fnGroupId
			$(selfInputTag).eq(2).val(fnParentId);//fnParentId
			$(selfInputTag).eq(4).val(fnParentIdx);//fnParentIdx
			$(selfInputTag).eq(6).val(fnGroupId);//fnGroupId
		}
		
		$(selfInputTag).each(function(){
			var inputNm = $(this).attr("name");
			if(inputNm != null && inputNm != "undefined" && inputNm != ""){
				var suffix = inputNm.substring(inputNm.indexOf("].")+1);
				$(this).attr("name", "items["+(idx)+"]"+suffix);
			}
		});
	});
};

jqSortable.prototype.getView = function(){
	var obj = this;
	var viewTargetObj = obj.opts["viewTargetObj"];
	var data = obj.opts["viewData"];
	
	var ajax = new jqMyAjax();
	var option = new Object();
	option["url"] = obj.opts["viewUrl"];
	option["dataType"] = obj.opts["viewDataType"];
	option["type"] = obj.opts["viewType"];
	option["data"] = (data != null && data.indexOf("frm:") != -1)? $("#"+data.split("frm:")[1]).serialize() : data;
	option["success"] = function(data){
		$(viewTargetObj).html(data);
	};
	option["complete"] = function(){ 
		ajax.uiUnBlock();
	};
	option["error"] = function(xhr, status, thrown){
		var extraMsg = "";
		var sessionOutRedirect = true;
		ajax.errorCalback(xhr, extraMsg, sessionOutRedirect);
	};
	
	ajax.callAjax(option);
};
